package com.woniu.qqmaster.utils;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@ControllerAdvice
public class MyExceptionHandler {


    @ExceptionHandler(NullPointerException.class)
    public void nullExcetion(NullPointerException ex, HttpServletResponse resp) throws IOException, IOException {

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        // 这段代码不是在服务器端执行了.... 客户端浏览器执行的。。。
        writer.write("<h1>非法登录，系统将在<span id='tx'></span>跳转到登录页面......</h1> " +
                "<script> let m = 5; let t = setInterval(function() { if(m <= 0) { clearInterval(t); window.location.href='/login.html' } document.querySelector('#tx').innerHTML = m--; } , 1000) " +
                "</script>");
        writer.flush();
        writer.close();
    }
}